﻿using EnterpriseWebsiteEntity;
using EnterpriseWebsiteUtil.Helper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace EnterpriseWebsiteBLL
{
    public class AboutUsService
    {
        public readonly SqlSugarClient _db;

        public AboutUsService(SqlSugarHelper sqlSugarHelper)
        {
            _db = sqlSugarHelper.sugarClient;
        }

        /// <summary>
        /// 添加一个新的AboutUsEntity实例到数据库，并且将CreateDate属性设置为当前时间。
        /// </summary>
        /// <param name="entity">要添加的AboutUsEntity实例</param>
        /// <returns>影响的行数</returns>
        public async Task<int> AddAsync(AboutUsEntity entity)
        {
            if (!string.IsNullOrEmpty(entity.HomeContent) && entity.HomeContent.Length > 300)
            {
                throw new Exception("首页展示中文内容不能大于300个字数");
            }
            if (!string.IsNullOrEmpty(entity.HomeContentEn) && entity.HomeContentEn.Length > 600)
            {
                throw new Exception("首页展示英文内容不能大于600个字数");
            }
            entity.CreateDate = DateTime.Now;
            return await _db.Insertable(entity).ExecuteCommandAsync();
        }

        /// <summary>
        /// 根据指定的Id获取AboutUsEntity实例。
        /// </summary>
        /// <param name="Id">指定的Id</param>
        /// <returns>获取到的AboutUsEntity实例，如果不存在则返回null。</returns>
        public async Task<AboutUsEntity> GetByIdAsync(int Id)
        {
            return await _db.Queryable<AboutUsEntity>().Where(a => a.Id == Id).FirstAsync();
        }

        /// <summary>
        /// 根据指定的Id更新AboutUsEntity实例的Content属性。
        /// </summary>
        /// <param name="Id">指定的Id</param>
        /// <param name="content">要更新的Content属性</param>
        /// <returns>影响的行数</returns>
        public async Task<int> UpdateContentAsync(int Id, string content,string contentEn)
        {
            var entity = await GetByIdAsync(Id);
            if (entity == null)
            {
                throw new Exception("数据不存在");
            }
            entity.Content = content;
            entity.ContentEn = contentEn;
            return await _db.Updateable(entity).ExecuteCommandAsync();
        }

        public async Task<int> UpdateContentAsync(AboutUsEntity entity)
        {
            if (!string.IsNullOrEmpty(entity.HomeContent) && entity.HomeContent.Length > 300)
            {
                throw new Exception("首页展示中文内容不能大于300个字数");
            }
            if (!string.IsNullOrEmpty(entity.HomeContentEn) && entity.HomeContentEn.Length > 600)
            {
                throw new Exception("首页展示英文内容不能大于600个字数");
            }
            return await _db.Updateable(entity).ExecuteCommandAsync();
        }

        /// <summary>
        /// 根据指定的Id删除AboutUsEntity实例。
        /// </summary>
        /// <param name="Id">指定的Id</param>
        /// <returns>影响的行数</returns>
        public async Task<int> DeleteByIdAsync(int Id)
        {
            return await _db.Deleteable<AboutUsEntity>().Where(a => a.Id == Id).ExecuteCommandAsync();
        }

        /// <summary>
        /// 获取所有的AboutUsEntity实例。
        /// </summary>
        /// <returns>所有的AboutUsEntity实例</returns>
        public async Task<List<AboutUsEntity>> GetAllAsync(bool? homeDisplay = null)
        {
            return await _db.Queryable<AboutUsEntity>()
                .WhereIF(homeDisplay != null,a=> a.HomeDisplay == homeDisplay)
                .ToListAsync();
        }
    }
}
